package com.guigu.ababa.ababaclassroom.web;

import com.alibaba.fastjson.JSONObject;
import com.guigu.ababa.ababaclassroom.entity.Ab_lecturer;
import com.guigu.ababa.ababaclassroom.entity.Denglu;
import com.guigu.ababa.ababaclassroom.service.Ab_lecturerService;
import com.guigu.ababa.ababaclassroom.util.CookieUtil;
import com.guigu.ababa.ababaclassroom.util.StoreInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LecturerCollertion {

    @Autowired
    Ab_lecturerService ab_lecturerService;


    //Redis接口
    @Autowired
    private StoreInterface redisStore;

    //Test单点登陆
    private static final String USER_KEY="user_key";

    //查询全部教师
    @GetMapping("findlecturer")
    @ResponseBody
    public List<Ab_lecturer> findlecturer(){
        return  ab_lecturerService.Selectlecturer();
    }

    //查询单个教师
    @GetMapping("findByidturer/{userid}")
    @ResponseBody
    public Ab_lecturer findoneId(@PathVariable("userid") int userid){
        return  ab_lecturerService.SelectoneId(userid);
    }

    //修改教师
    @RequestMapping("updatelecturer")
    @ResponseBody
    public int updatelecturer(@RequestBody Ab_lecturer ab_lecturer){
       return ab_lecturerService.updatelecturer(ab_lecturer);
    }


    //redis测试单点登陆
    @RequestMapping("/login")
    public Object login(String username, String password, HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        redisStore.set(CookieUtil.getLoginCookie(request,response,true),map);

        return map+",";
    }

    //redis测试单点登陆
    @RequestMapping("/getUser1")
    public  Object getUser(HttpServletRequest request,HttpServletResponse response){
        Object user =request.getSession().getAttribute(USER_KEY);
        Object o = redisStore.get(CookieUtil.getLoginCookie(request, response, false));
        System.out.println(o);
        HashMap hashMap1 = JSONObject.parseObject(JSONObject.toJSONString(o), HashMap.class);
        System.out.println(hashMap1.get("username"));
        return user+","+o+",";
    }
}
